package com.chj.comp.service.demo1.service.impl;

import com.alibaba.fastjson.JSON;
import com.chj.comp.common.core.entity.MqMessageInfo;
import com.chj.comp.common.core.feign.RemoteMqadminService;
import com.chj.comp.common.rabbitmq.constant.MsgStatusEnum;
import com.chj.comp.common.rabbitmq.constant.MsgTypeEnum;
import com.chj.comp.common.rabbitmq.message.MessageEntity;
import com.chj.comp.common.rabbitmq.queue.MqSimpleQueue;
import com.chj.comp.common.rabbitmq.sender.*;
import com.chj.comp.common.rabbitmq.util.MessageUtil;
import com.chj.comp.service.demo1.service.IRabbitmqTestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author ：cuihui
 * @date ：Created in 2022/6/8
 * @description：es接口示例
 * @modified By：
 */
@Service
@Slf4j
public class RabbitmqTestService implements IRabbitmqTestService {

    @Resource
    private RemoteMqadminService remoteMqadminService;

    @Resource
    private MqSimpleSender mqSimpleSender;

    @Resource
    private MqWorkSender mqWorkSender;

    @Resource
    private MqFanoutSender mqFanoutSender;

    @Resource
    private MqDirectSender mqDirectSender;

    @Resource
    private MqTopicSender mqTopicSender;

    @Override
    public void sendSimpleMsg() {
        log.info("---生产消息（简单队列）---");
        for (int i = 0; i < 3; i++) {
            // STEP1：生产者生成消息
            MessageEntity messageEntity = MessageEntity.builder().name("cuihui" + i).phone("15502986569").card("610523199112153316").build();
            Message message = MessageUtil.createMessage(messageEntity);
            // STEP2：保存消息
            MessageProperties messageProperties = message.getMessageProperties();
            MqMessageInfo mqMessageInfo = MqMessageInfo.builder().messageId(messageProperties.getMessageId())
                    .type(MsgTypeEnum.TYPE_1.getType())
                    .queue(MqSimpleQueue.QUEUE_NAME)
                    .messageInfo(JSON.toJSONString(message))
                    .messageBody(JSON.toJSONString(messageEntity))
                    .status(MsgStatusEnum.STATUS_0.getStatus())
                    .sendNum(0).build();
            remoteMqadminService.addMqMessageInfo(mqMessageInfo);
            // STEP3：发送消息
            String sendStatus = mqSimpleSender.send(message);
            // STEP4：更新消息状态为已发送、发送次数+1
            if(StringUtils.isNotEmpty(sendStatus)){
                remoteMqadminService.updateMqMessageStatus(messageProperties.getMessageId(), MsgStatusEnum.STATUS_1.getStatus());
                remoteMqadminService.addMqMessageSendNum(messageProperties.getMessageId());
            }
        }
    }

    @Override
    public void sendWorkMsg() {
        log.info("---生产消息（工作队列）---");
        for (int i = 0; i < 3; i++) {
            // STEP1：生产者生成消息
            MessageEntity messageEntity = MessageEntity.builder().name("cuihui" + i).phone("15502986569").card("610523199112153316").build();
            Message message = MessageUtil.createMessage(messageEntity);
            // STEP2：保存消息

            // STEP3：发送消息
            mqWorkSender.send(message);
        }
    }

    @Override
    public void sendFanoutMsg() {
        log.info("---生产消息（发布订阅模型-广播类型）---");
        for (int i = 0; i < 3; i++) {
            // STEP1：生产者生成消息
            MessageEntity messageEntity = MessageEntity.builder().name("cuihui" + i).phone("15502986569").card("610523199112153316").build();
            Message message = MessageUtil.createMessage(messageEntity);
            // STEP2：保存消息

            // STEP3：发送消息
            mqFanoutSender.send(message);
        }
    }

    @Override
    public void sendDirectMsg() {
        log.info("---生产消息（发布订阅模型-路由类型）---");
        for (int i = 0; i < 3; i++) {
            // STEP1：生产者生成消息
            MessageEntity messageEntity = MessageEntity.builder().name("cuihui" + i).phone("15502986569").card("610523199112153316").build();
            Message message = MessageUtil.createMessage(messageEntity);
            // STEP2：保存消息

            // STEP3：发送消息
            mqDirectSender.send(message);
        }
    }

    @Override
    public void sendTopicMsg() {
        log.info("---生产消息（发布订阅模型-主题类型）---");
        for (int i = 0; i < 3; i++) {
            // STEP1：生产者生成消息
            MessageEntity messageEntity = MessageEntity.builder().name("cuihui" + i).phone("15502986569").card("610523199112153316").build();
            Message message = MessageUtil.createMessage(messageEntity);
            // STEP2：保存消息

            // STEP3：发送消息
            mqTopicSender.send(message);
        }
    }
}
